﻿using DailyApp.WPF.DTOs;
using DailyApp.WPF.HttpClients;
using DailyApp.WPF.MsgEvents;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace DailyApp.WPF.ViewModels
{
    /// <summary>
    /// 登录视图模型
    /// </summary>
    internal class LoginUserControlViewModel : BindableBase, IDialogAware
    {
        public string Title { get; set; } = "我的日常";

        public event Action<IDialogResult> RequestClose;
        private readonly HttpRestClient HttpClient;
        private readonly IEventAggregator eventAggregator;

        /// <summary>
        /// 登录命令
        /// </summary>
        public DelegateCommand LoginCmm { get; set; }

        public LoginUserControlViewModel(HttpRestClient _HttpClient, IEventAggregator _eventAggregator)
        {
            //命令
            LoginCmm = new DelegateCommand(Login);
            ShowRegInfoCmm = new DelegateCommand(ShowRegInfo);
            ShowLogInfoCmm = new DelegateCommand(ShowLogInfo);
            RegCmm = new DelegateCommand(Reg);
            ///实例化注册信息
            AccountInfoDTO = new AccountInfoDTO();
            //请求client
            HttpClient = _HttpClient;
            //发布订阅
            eventAggregator= _eventAggregator;
        }
        /// <summary>
        /// 登录方法
        /// </summary>
        private void Login()
        {
            if (string.IsNullOrEmpty(Account) || string.IsNullOrEmpty(Pwd))
            {
                //发布消息
                eventAggregator.GetEvent<MsgEvent>().Publish("登录信息不全");
                return;
            }
            //调用api
            ApiRequest apiRequest = new ApiRequest();
            apiRequest.Method = RestSharp.Method.GET;
            Pwd=Md5Helper.GetMd5_16(Pwd);
            apiRequest.Route = $"Users/Login?Badge={Account}&pwd={Pwd}";
           ApiResponse response= HttpClient.Execute(apiRequest);

            if (response.ResultCode == 1)
            {
                if (RequestClose != null)
                {
                    //将json格式反序列化对象
                    AccountInfoDTO accountInfoDTO= JsonConvert.DeserializeObject<AccountInfoDTO>(response.ResultData.ToString());

                    DialogParameters paras= new DialogParameters();
                    paras.Add("LoginName", accountInfoDTO.Cname);
                    RequestClose(new DialogResult(ButtonResult.OK,paras));
                }
            }
            else
            {
                eventAggregator.GetEvent<MsgEvent>().Publish(response.Msg);
            }
        }

        #region 注册
        public DelegateCommand RegCmm { get; set; }
        /// <summary>
        /// 注册方法
        /// </summary>
        private void Reg()
        {
            if (string.IsNullOrEmpty(AccountInfoDTO.Badge) || string.IsNullOrEmpty(AccountInfoDTO.Dept)
                || string.IsNullOrEmpty(AccountInfoDTO.Password) || string.IsNullOrEmpty(AccountInfoDTO.ConfirmPassword))
            {
                eventAggregator.GetEvent<MsgEvent>().Publish("注册信息不全");
                //MessageBox.Show("注册信息不全");
                return;
            }
            if (AccountInfoDTO.Password != AccountInfoDTO.ConfirmPassword)
            {
                eventAggregator.GetEvent<MsgEvent>().Publish("两次密码不同");
                //MessageBox.Show("两次密码不同");
                return;
            }

            //调用API
            ApiRequest apiRequest = new ApiRequest();
            apiRequest.Method = RestSharp.Method.POST;
            apiRequest.Route = "Users/Reg";

            //密码处理
            AccountInfoDTO.Password = Md5Helper.GetMd5_16(AccountInfoDTO.Password);
            AccountInfoDTO.ConfirmPassword = Md5Helper.GetMd5_16(AccountInfoDTO.ConfirmPassword);

            apiRequest.Parameters = AccountInfoDTO;

            ApiResponse apiResponse = HttpClient.Execute(apiRequest);//执行请求
            if (apiResponse.ResultCode == 1)
            {
                eventAggregator.GetEvent<MsgEvent>().Publish(apiResponse.Msg);
                //MessageBox.Show(apiResponse.Msg);
                SelectedIndex = 0;
            }
            else
            {
                eventAggregator.GetEvent<MsgEvent>().Publish(apiResponse.Msg);
                //MessageBox.Show(apiResponse.Msg);
            }
        }
        private AccountInfoDTO _accountInfoDTO;
        /// <summary>
        /// 注册信息
        /// </summary>
        public AccountInfoDTO AccountInfoDTO
        {
            get { return _accountInfoDTO; }
            set
            {
                _accountInfoDTO = value;
                RaisePropertyChanged();
            }
        }

        #endregion

        /// <summary>
        /// 是否可以关闭对话框
        /// </summary>
        /// <returns></returns>
        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {

        }

        #region 显示内容

        private int _SelectedIndex;
        /// <summary>
        /// 显示内容的索引
        /// </summary>
        public int SelectedIndex
        {
            get { return _SelectedIndex; }
            set
            {
                _SelectedIndex = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 命令
        /// </summary>
        public DelegateCommand ShowRegInfoCmm { get; set; }
        /// <summary>
        /// 显示注册信息
        /// </summary>
        private void ShowRegInfo()
        {
            SelectedIndex = 1;
        }

        /// <summary>
        /// 显示登录信息
        /// </summary>
        public DelegateCommand ShowLogInfoCmm { get; set; }
        private void ShowLogInfo()
        {
            SelectedIndex = 0;
        }
        #endregion

        #region 登录信息
        private string _Account;
        /// <summary>
        /// 账号
        /// </summary>
        public string Account
        {
            get { return _Account; }
            set
            {
                _Account = value;
                RaisePropertyChanged();
            }
        }
        private string _Pwd;
        /// <summary>
        /// 密码
        /// </summary>
        public string Pwd
        {
            get { return _Pwd; }
            set {
                _Pwd = value;
                RaisePropertyChanged();
            }
        }
        #endregion


    }
}
